package com.training.schedule_management_system_admin.service;

import com.training.schedule_management_system_admin.entity.*;
import com.training.schedule_management_system_admin.mapper.ScheduleMapper;
import com.training.schedule_management_system_admin.mapper.ScheduletableMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ScheduletableService {

    @Autowired
    ScheduletableMapper scheduletableMapper;

    @Autowired
    ScheduleMapper scheduleMapper;

    /*
    获取当前用户下所有的日程表
     */
    public List<Scheduletable> getAllWithUser() {
        //获取当前已登录的用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        ScheduletableExample example=new ScheduletableExample();
        ScheduletableExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(currentUser.getId());
        example.setOrderByClause("open_state desc,name asc");
        return scheduletableMapper.selectByExample(example);
    }

    /*
    根据日程表id得到日程表
     */
    public Scheduletable getTableById(Integer scheduletableId) {
        return scheduletableMapper.selectByPrimaryKey(scheduletableId);
    }


    /*
    日程表公开/私有
     */
    public int state(Scheduletable table) {
        return scheduletableMapper.updateByPrimaryKeySelective(table);
    }

    /*
    日程表的删除
     */
    public int delete(Integer scheduletableId) {
        return scheduletableMapper.deleteByPrimaryKey(scheduletableId);
    }

    /*
    创建日程表
     */
    public int create(Scheduletable table) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        table.setOpenState(0);
        table.setUserId(currentUser.getId());
        return scheduletableMapper.insertSelective(table);
    }

    /*
    修改日程表
     */
    public int update(Scheduletable table) {
        return scheduletableMapper.updateByPrimaryKeySelective(table);
    }

    /*
    模糊查询
     */
    public List<Scheduletable> findTableLike(String value) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        ScheduletableExample example=new ScheduletableExample();
        ScheduletableExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(currentUser.getId());
        criteria.andNameLike("%"+value+"%");
        List<Scheduletable> tables = scheduletableMapper.selectByExample(example);
        if(tables!=null && !tables.isEmpty()) {
            return tables;
        }else{
            return Collections.emptyList();
        }
    }

    /*
    复制
     */
    public void copy(Scheduletable table) {
        Scheduletable scheduletable=new Scheduletable();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        BeanUtils.copyProperties(table,scheduletable);
        scheduletable.setId(null);
        scheduletable.setName(table.getName()+"-副本");
        scheduletable.setOpenState(0);
        scheduletable.setUserId(currentUser.getId());
        //先插入日程表
        scheduletableMapper.insertSelective(scheduletable);


        //日程
        //如果日程不为空才执行
        if(scheduletable.getSchedules()!=null && !scheduletable.getSchedules().isEmpty()) {
            List<Schedule> list = new ArrayList<>();
            for (Schedule schedule : scheduletable.getSchedules()) {
                schedule.setId(null);
                schedule.setScheduletableId(scheduletable.getId());
                list.add(schedule);
            }
            //批量插入日程
            scheduleMapper.insertList(list);
        }
    }
}
